package com.roc.steelend.service.Impl;

import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.roc.steelend.controller.vo.common.*;
import com.roc.steelend.controller.vo.returnformat.ReturnMsg;
import com.roc.steelend.controller.vo.sale.JuanBanSaleOrderVo;
import com.roc.steelend.controller.vo.sale.OtherProductsSaleOrderVo;
import com.roc.steelend.controller.vo.sale.PingBanSaleOrderVo;
import com.roc.steelend.mapper.ClientMapper;
import com.roc.steelend.mapper.InventoryMapper;
import com.roc.steelend.mapper.SaleDetailOrderMapper;
import com.roc.steelend.mapper.SaleOrderMapper;
import com.roc.steelend.pojo.*;
import com.roc.steelend.service.SaleOrderService;
import com.roc.steelend.util.LshUtil;
import org.apache.logging.log4j.spi.CleanableThreadContextMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.crypto.SealedObject;
import javax.print.attribute.SetOfIntegerSyntax;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.*;

@Service
@Transactional(rollbackFor = Exception.class)
public class SaleOrderServiceImpl implements SaleOrderService {

    @Autowired
    private ClientMapper clientMapper;
    @Autowired
    private SaleOrderMapper saleOrderMapper;
    @Autowired
    private SaleDetailOrderMapper saleDetailOrderMapper;
    @Autowired
    private InventoryMapper inventoryMapper;

    //  平板库存查询wrapper
    private static QueryWrapper<Inventory> PingBanWrapperUtil(SaleDetail saleDetail, Integer otherwarehouseId,Integer warehouseId ) {
        QueryWrapper<Inventory> wrapper = new QueryWrapper<>();
        if (otherwarehouseId!=null){
            wrapper.ne("warehouse_id",otherwarehouseId);
        }else {
            wrapper.eq("warehouse_id",warehouseId);
        }

        wrapper.eq("small_type_id",saleDetail.getSmallTypeId());
        wrapper.eq("thickness",saleDetail.getThickness());
        wrapper.eq("long1",saleDetail.getLong1());
        wrapper.eq("wide",saleDetail.getWide());
        wrapper.eq("high",saleDetail.getHigh());
        wrapper.orderByDesc("quantity");
        return wrapper;
    }
    //  卷板库存查询wrapper
    private static QueryWrapper<Inventory> JuanBanWrapperUtil(SaleDetail saleDetail, Integer otherwarehouseId,Integer warehouseId) {
        QueryWrapper<Inventory> wrapper = new QueryWrapper<>();
        if (otherwarehouseId!=null){
            wrapper.ne("warehouse_id",otherwarehouseId);
        }else {
            wrapper.eq("warehouse_id",warehouseId);
        }

        wrapper.eq("small_type_id",saleDetail.getSmallTypeId());
        wrapper.eq("thickness",saleDetail.getThickness());
        wrapper.eq("weight",saleDetail.getWeight());
        return wrapper;
    }
    //    其他产品库存查询wrapper
    private static QueryWrapper<Inventory> OtherProductsWrapperUtil(SaleDetail saleDetail,Integer otherwarehouseId, Integer warehouseId) {
        QueryWrapper<Inventory> wrapper = new QueryWrapper<>();
        if (otherwarehouseId!=null){
            wrapper.ne("warehouse_id",otherwarehouseId);
        }else {
            wrapper.eq("warehouse_id",warehouseId);
        }
        wrapper.eq("small_type_id",saleDetail.getSmallTypeId());
        wrapper.eq("specification",saleDetail.getSpecification());
        return wrapper;
    }

    @Override
    public ReturnMsg addSalePingBanOrderService(PingBanSaleOrderVo pingBanSaleOrderVo, Integer employeeId) {
//        1、判断是否要注册新的客户
        Boolean tempByClientInsert=false;
        if(pingBanSaleOrderVo.getIsNewClient()){
            Map<String, Object> clientMap = pingBanSaleOrderVo.getOtherMsg().getClient();
            Client client = new Client();
            client.setClientName(clientMap.get("clientName").toString());
            client.setGroupId(Integer.valueOf(clientMap.get("groupId").toString()));
            client.setSex(String.valueOf(clientMap.get("sex")));
            client.setInvoice(new BigDecimal("0"));
            if (clientMap.get("phone")==null){
                client.setPhone(null);
            }else {
                client.setPhone(new BigInteger(clientMap.get("phone").toString()));
            }
            if (clientMap.get("qq")==null){
                client.setQq(null);
            }else {
                client.setQq(new BigInteger(clientMap.get("qq").toString()));
            }
            if (clientMap.get("email")==null){
                client.setEmail(null);
            }else {
                client.setEmail(String.valueOf(clientMap.get("email")));
            }
            if (clientMap.get("address")==null){
                client.setAddress(null);
            }else {
                client.setAddress(String.valueOf(clientMap.get("address")));
            }
            if (clientMap.get("remark")==null){
                client.setRemark(null);
            }else {
                client.setRemark(String.valueOf(clientMap.get("remark")));
            }
            client.setDebt(new BigDecimal("0"));
            client.setSignupTime(new Timestamp(new Date().getTime()));
            int insert = clientMapper.insert(client);
            if (insert==1){
                tempByClientInsert=true;
            }
        }

        Integer clientId=null;
        if (pingBanSaleOrderVo.getIsNewClient()){
            QueryWrapper<Client> wrapper = new QueryWrapper<>();
            wrapper.orderByDesc("client_id");
            wrapper.last("LIMIT 1");
            List<Client> clients = clientMapper.selectList(wrapper);
             clientId = clients.get(0).getClientId();
        }else {
            tempByClientInsert=true;
            clientId=Integer.valueOf(pingBanSaleOrderVo.getOtherMsg().getClient().get("client_id").toString());

        }

        ReturnMsg msg = new ReturnMsg();
        if (tempByClientInsert){
//            1、进行客户扣款， 计算总共要多少钱(是要包括运费的)
            BigDecimal freight = pingBanSaleOrderVo.getOtherMsg().getFreight();
            BigDecimal count = pingBanSaleOrderVo.getOtherMsg().getCount();
            BigDecimal clientNewDebt =freight.add(count);
            clientMapper.updateCLientDebt(clientId,clientNewDebt);
//            2、操作销售订单大类 生成订单号，员工id,客户id,运费总价(不含运费),操作订单时间，出库时间，备注
            BigInteger saleLsh = LshUtil.getSaleNo1TypeLsh();
            Timestamp operateTime = new Timestamp(new Date().getTime());
            java.sql.Date outStorageTime = pingBanSaleOrderVo.getOtherMsg().getOrderTime();
            String remark = pingBanSaleOrderVo.getOtherMsg().getRemark();
            SaleOrder saleOrder = new SaleOrder();
            saleOrder.setSaleOrderId(saleLsh);
            saleOrder.setEmployeeId(employeeId);
            saleOrder.setSumMoney(clientNewDebt);
            saleOrder.setInvoiceMoney(clientNewDebt);
            saleOrder.setInvoiceIsSettled(0);
            saleOrder.setClientId(clientId);
            saleOrder.setFreight(freight);
            saleOrder.setRemark(remark);
            saleOrder.setOutStorageDate(outStorageTime);
            saleOrder.setOperateTime(operateTime);
            saleOrder.setPaymentIsSettled(0);
            saleOrder.setOrderTotalMoney(count);
            saleOrder.setGiven(new BigDecimal("0"));
            saleOrder.setIsReconciliation(0);
            saleOrder.setIsWriteOff(0);
            saleOrder.setIsInvoice(0);
            saleOrder.setIsTiaoPiao(0);
            saleOrder.setTiaoPiaoId(null);
            saleOrder.setMoLingTou(0);
            saleOrderMapper.insert(saleOrder);
//            3、操作销售订单细节表,需要当前大表的流水号
            List<PingBanSaleBuyOrderDetailVo> pingBanSaleOrderDetailList = pingBanSaleOrderVo.getSalePingBanOrderDetailList();
            ArrayList<SaleDetail> saleDetailArrayList = new ArrayList<>();
            for (int i = 0; i <pingBanSaleOrderDetailList.size() ; i++) {
                PingBanSaleBuyOrderDetailVo orderDetailVo = pingBanSaleOrderDetailList.get(i);
                SaleDetail saleDetail = new SaleDetail();
                saleDetail.setSaleOrderId(saleLsh);
                saleDetail.setSmallTypeId(orderDetailVo.getLeibie());
                saleDetail.setSpecification(orderDetailVo.getHoudu().toString());
                saleDetail.setThickness(orderDetailVo.getHoudu());
                saleDetail.setLong1(orderDetailVo.getLong1());
                saleDetail.setWide(orderDetailVo.getWide());
                saleDetail.setHigh(orderDetailVo.getHeight());
                saleDetail.setQuantity(orderDetailVo.getQuantity());
                saleDetail.setPrice(orderDetailVo.getPrice());
                saleDetail.setWeight(orderDetailVo.getWeight());
                saleDetail.setWay(Integer.valueOf(orderDetailVo.getWay()));
                saleDetailArrayList.add(saleDetail);
            }

            for (int i = 0; i <saleDetailArrayList.size() ; i++) {
                SaleDetail saleDetail = saleDetailArrayList.get(i);
                saleDetailOrderMapper.insert(saleDetail);
            }
//            4、对库存表进行一系列的更新
            for (int i = 0; i <saleDetailArrayList.size() ; i++) {
                SaleDetail saleDetail = saleDetailArrayList.get(i);
                QueryWrapper<Inventory> firstWrapper = PingBanWrapperUtil(saleDetail,3,null );
                List<Inventory> inventoryList = inventoryMapper.selectList(firstWrapper);
                if (!inventoryList.isEmpty()){
                    Integer temp=0;
                    Integer quantity = saleDetail.getQuantity();
                    for (int j = 0; j <inventoryList.size() ; j++) {
                        Inventory inventory = inventoryList.get(j);
                         temp = inventory.getQuantity() - quantity;
                        if (temp>0){
                            inventory.setQuantity(temp);
                            inventoryMapper.updateById(inventory);
                            break;
                        }else if (temp==0){
                            inventoryMapper.deleteById(inventory.getInventoryId());
                            break;
                        }else {
                            quantity=-temp;
                            inventoryMapper.deleteById(inventory.getInventoryId());
                        }
                    }
                    if (temp<0){
                        Inventory inventory2 = new Inventory();
                        inventory2.setSmallTypeId(saleDetail.getSmallTypeId());
                        inventory2.setWarehouseId(3);
                        inventory2.setSpecification(saleDetail.getSpecification());
                        inventory2.setThickness(saleDetail.getThickness());
                        inventory2.setLong1(saleDetail.getLong1());
                        inventory2.setWide(saleDetail.getWide());
                        inventory2.setHigh(saleDetail.getHigh());
                        inventory2.setQuantity(temp);
                        inventory2.setWeight(new BigDecimal(0));
                        inventoryMapper.insert(inventory2);
                    }

                }else {
                    QueryWrapper<Inventory> secondWrapper = PingBanWrapperUtil(saleDetail, null, 3);
                    List<Inventory> inventoryList2 = inventoryMapper.selectList(secondWrapper);
                    if (inventoryList2.isEmpty()){
                        Inventory inventory3 = new Inventory();
                        inventory3.setSmallTypeId(saleDetail.getSmallTypeId());
                        inventory3.setWarehouseId(3);
                        inventory3.setSpecification(saleDetail.getSpecification());
                        inventory3.setThickness(saleDetail.getThickness());
                        inventory3.setLong1(saleDetail.getLong1());
                        inventory3.setWide(saleDetail.getWide());
                        inventory3.setHigh(saleDetail.getHigh());
                        inventory3.setQuantity(-saleDetail.getQuantity());
                        inventory3.setWeight(new BigDecimal(0));
                        inventoryMapper.insert(inventory3);
                    }else {
                        Inventory inventory4 = inventoryList2.get(0);
                        Integer oldQuantity = inventory4.getQuantity();
                        Integer newQuantity=oldQuantity-saleDetail.getQuantity();
                        inventory4.setQuantity(newQuantity);
                        inventoryMapper.updateById(inventory4);
                    }
                }

            }
            msg.setMsg("添加平板销售订单成功");
            msg.setCode(1);
            return  msg;
        }else {
            msg.setMsg("系统好像睡着了，注册新供应商失败");
            msg.setCode(0);
            return msg;
        }

    }

    @Override
    public ReturnMsg addSaleJuanBanOrderService(JuanBanSaleOrderVo juanBanSaleOrderVo, Integer employeeId) {
        //        1、判断是否要注册新的客户
        Boolean tempByClientInsert=false;
        if(juanBanSaleOrderVo.getIsNewClient()){
            Map<String, Object> clientMap = juanBanSaleOrderVo.getOtherMsg().getClient();
            Client client = new Client();
            client.setClientName(clientMap.get("clientName").toString());
            client.setGroupId(Integer.valueOf(clientMap.get("groupId").toString()));
            client.setSex(String.valueOf(clientMap.get("sex")));
            if (clientMap.get("phone")==null){
                client.setPhone(null);
            }else {
                client.setPhone(new BigInteger(clientMap.get("phone").toString()));
            }
            if (clientMap.get("qq")==null){
                client.setQq(null);
            }else {
                client.setQq(new BigInteger(clientMap.get("qq").toString()));
            }
            if (clientMap.get("email")==null){
                client.setEmail(null);
            }else {
                client.setEmail(String.valueOf(clientMap.get("email")));
            }
            if (clientMap.get("address")==null){
                client.setAddress(null);
            }else {
                client.setAddress(String.valueOf(clientMap.get("address")));
            }
            if (clientMap.get("remark")==null){
                client.setRemark(null);
            }else {
                client.setRemark(String.valueOf(clientMap.get("remark")));
            }
            client.setDebt(new BigDecimal("0"));
            client.setInvoice(new BigDecimal("0"));
            client.setSignupTime(new Timestamp(new Date().getTime()));
            int insert = clientMapper.insert(client);
            if (insert==1){
                tempByClientInsert=true;
            }
        }

        Integer clientId=null;
        if (juanBanSaleOrderVo.getIsNewClient()){
            QueryWrapper<Client> wrapper = new QueryWrapper<>();
            wrapper.orderByDesc("client_id");
            wrapper.last("LIMIT 1");
            List<Client> clients = clientMapper.selectList(wrapper);
            clientId = clients.get(0).getClientId();
        }else {
            tempByClientInsert=true;
            clientId=Integer.valueOf(juanBanSaleOrderVo.getOtherMsg().getClient().get("client_id").toString());

        }


        ReturnMsg msg = new ReturnMsg();
        if (tempByClientInsert){
//            1、进行客户扣款， 计算总共要多少钱(是要包括运费的)
            BigDecimal freight = juanBanSaleOrderVo.getOtherMsg().getFreight();
            BigDecimal count = juanBanSaleOrderVo.getOtherMsg().getCount();
            BigDecimal clientNewDebt =freight.add(count);
            clientMapper.updateCLientDebt(clientId,clientNewDebt);
//            2、操作销售订单大类 生成订单号，员工id,客户id,运费总价(不含运费),操作订单时间，出库时间，备注
            BigInteger saleLsh = LshUtil.getSaleNo2TypeLsh();
            Timestamp operateTime = new Timestamp(new Date().getTime());
            java.sql.Date outStorageTime = juanBanSaleOrderVo.getOtherMsg().getOrderTime();
            String remark = juanBanSaleOrderVo.getOtherMsg().getRemark();
            SaleOrder saleOrder = new SaleOrder();
            saleOrder.setSaleOrderId(saleLsh);
            saleOrder.setEmployeeId(employeeId);
            saleOrder.setSumMoney(clientNewDebt);
            saleOrder.setInvoiceMoney(clientNewDebt);
            saleOrder.setInvoiceIsSettled(0);
            saleOrder.setClientId(clientId);
            saleOrder.setFreight(freight);
            saleOrder.setRemark(remark);
            saleOrder.setOutStorageDate(outStorageTime);
            saleOrder.setOperateTime(operateTime);
            saleOrder.setPaymentIsSettled(0);
            saleOrder.setOrderTotalMoney(count);
            saleOrder.setGiven(new BigDecimal("0"));
            saleOrder.setIsReconciliation(0);
            saleOrder.setIsWriteOff(0);
            saleOrder.setIsInvoice(0);
            saleOrder.setIsTiaoPiao(0);
            saleOrder.setTiaoPiaoId(null);
            saleOrder.setMoLingTou(0);
            saleOrderMapper.insert(saleOrder);
//            3、操作销售订单细节表,需要当前大表的流水号
            List<JuanBanSaleBuyOrderDetailVo> juanBanSaleOrderDetailList = juanBanSaleOrderVo.getSaleJuanBanOrderDetailList();
            ArrayList<SaleDetail> saleDetailArrayList = new ArrayList<>();
            for (int i = 0; i <juanBanSaleOrderDetailList.size() ; i++) {
                JuanBanSaleBuyOrderDetailVo orderDetailVo = juanBanSaleOrderDetailList.get(i);
                SaleDetail saleDetail = new SaleDetail();
                saleDetail.setSaleOrderId(saleLsh);
                saleDetail.setSmallTypeId(orderDetailVo.getLeibie());
                saleDetail.setSpecification(orderDetailVo.getHoudu().toString());
                saleDetail.setThickness(orderDetailVo.getHoudu());
                saleDetail.setLong1(new BigDecimal("0"));
                saleDetail.setWide(new BigDecimal("0"));
                saleDetail.setHigh(new BigDecimal("0"));
                saleDetail.setQuantity(1);
                saleDetail.setPrice(orderDetailVo.getPrice());
                saleDetail.setWeight(orderDetailVo.getWeight());
                saleDetail.setWay(1);
                saleDetailArrayList.add(saleDetail);
            }

            for (int i = 0; i <saleDetailArrayList.size() ; i++) {
                SaleDetail saleDetail = saleDetailArrayList.get(i);
                saleDetailOrderMapper.insert(saleDetail);
            }
//            4、对库存表进行一系列的更新
            for (int i = 0; i <saleDetailArrayList.size() ; i++) {
                SaleDetail saleDetail = saleDetailArrayList.get(i);
                QueryWrapper<Inventory> firstWrapper = JuanBanWrapperUtil(saleDetail,3,null );
                List<Inventory> inventoryList = inventoryMapper.selectList(firstWrapper);
                if (!inventoryList.isEmpty()){
                        Inventory inventory = inventoryList.get(0);
                       Integer temp = inventory.getQuantity() - 1;
                        if (temp>0){
                            inventory.setQuantity(temp);
                            inventoryMapper.updateById(inventory);
                        }else if (temp==0){
                            inventoryMapper.deleteById(inventory.getInventoryId());
                        }
                }else {
                    QueryWrapper<Inventory> secondWrapper = JuanBanWrapperUtil(saleDetail, null, 3);
                    List<Inventory> inventoryList2 = inventoryMapper.selectList(secondWrapper);
                    if (inventoryList2.isEmpty()){
                        Inventory inventory3 = new Inventory();
                        inventory3.setSmallTypeId(saleDetail.getSmallTypeId());
                        inventory3.setWarehouseId(3);
                        inventory3.setSpecification(saleDetail.getSpecification());
                        inventory3.setThickness(saleDetail.getThickness());
                        inventory3.setLong1(new BigDecimal("0"));
                        inventory3.setWide(new BigDecimal("0"));
                        inventory3.setHigh(new BigDecimal("0"));
                        inventory3.setQuantity(-1);
                        inventory3.setWeight(saleDetail.getWeight());
                        inventoryMapper.insert(inventory3);
                    }else {
                        Inventory inventory4 = inventoryList2.get(0);
                        Integer oldQuantity = inventory4.getQuantity();
                        Integer newQuantity=oldQuantity-1;
                        inventory4.setQuantity(newQuantity);
                        inventoryMapper.updateById(inventory4);
                    }
                }

            }
            msg.setMsg("添加卷板销售订单成功");
            msg.setCode(1);
            return  msg;
        }else {
            msg.setMsg("系统好像睡着了，注册新供应商失败");
            msg.setCode(0);
            return msg;
        }
    }

    @Override
    public ReturnMsg addSaleOtherProductsOrderService(OtherProductsSaleOrderVo otherProductsSaleOrderVo, Integer employeeId) {
        //        1、判断是否要注册新的客户
        Boolean tempByClientInsert=false;
        if(otherProductsSaleOrderVo.getIsNewClient()){
            Map<String, Object> clientMap = otherProductsSaleOrderVo.getOtherMsg().getClient();
            Client client = new Client();
            client.setClientName(clientMap.get("clientName").toString());
            client.setGroupId(Integer.valueOf(clientMap.get("groupId").toString()));
            client.setSex(String.valueOf(clientMap.get("sex")));
            if (clientMap.get("phone")==null){
                client.setPhone(null);
            }else {
                client.setPhone(new BigInteger(clientMap.get("phone").toString()));
            }
            if (clientMap.get("qq")==null){
                client.setQq(null);
            }else {
                client.setQq(new BigInteger(clientMap.get("qq").toString()));
            }
            if (clientMap.get("email")==null){
                client.setEmail(null);
            }else {
                client.setEmail(String.valueOf(clientMap.get("email")));
            }
            if (clientMap.get("address")==null){
                client.setAddress(null);
            }else {
                client.setAddress(String.valueOf(clientMap.get("address")));
            }
            if (clientMap.get("remark")==null){
                client.setRemark(null);
            }else {
                client.setRemark(String.valueOf(clientMap.get("remark")));
            }
            client.setDebt(new BigDecimal("0"));
            client.setInvoice(new BigDecimal("0"));
            client.setSignupTime(new Timestamp(new Date().getTime()));
            int insert = clientMapper.insert(client);
            if (insert==1){
                tempByClientInsert=true;
            }
        }

        Integer clientId=null;
        if (otherProductsSaleOrderVo.getIsNewClient()){
            QueryWrapper<Client> wrapper = new QueryWrapper<>();
            wrapper.orderByDesc("client_id");
            wrapper.last("LIMIT 1");
            List<Client> clients = clientMapper.selectList(wrapper);
            clientId = clients.get(0).getClientId();
        }else {
            tempByClientInsert=true;
            clientId=Integer.valueOf(otherProductsSaleOrderVo.getOtherMsg().getClient().get("client_id").toString());

        }

        ReturnMsg msg = new ReturnMsg();
        if (tempByClientInsert){
//            1、进行客户扣款， 计算总共要多少钱(是要包括运费的)
            BigDecimal freight = otherProductsSaleOrderVo.getOtherMsg().getFreight();
            BigDecimal count = otherProductsSaleOrderVo.getOtherMsg().getCount();
            BigDecimal clientNewDebt =freight.add(count);
            clientMapper.updateCLientDebt(clientId,clientNewDebt);
//            2、操作销售订单大类 生成订单号，员工id,客户id,运费总价(不含运费),操作订单时间，出库时间，备注
            BigInteger saleLsh = LshUtil.getSaleNo3TypeLsh();
            Timestamp operateTime = new Timestamp(new Date().getTime());
            java.sql.Date outStorageTime = otherProductsSaleOrderVo.getOtherMsg().getOrderTime();
            String remark = otherProductsSaleOrderVo.getOtherMsg().getRemark();
            SaleOrder saleOrder = new SaleOrder();
            saleOrder.setSaleOrderId(saleLsh);
            saleOrder.setEmployeeId(employeeId);
            saleOrder.setSumMoney(clientNewDebt);
            saleOrder.setInvoiceMoney(clientNewDebt);
            saleOrder.setInvoiceIsSettled(0);
            saleOrder.setClientId(clientId);
            saleOrder.setFreight(freight);
            saleOrder.setRemark(remark);
            saleOrder.setOutStorageDate(outStorageTime);
            saleOrder.setOperateTime(operateTime);
            saleOrder.setPaymentIsSettled(0);
            saleOrder.setOrderTotalMoney(count);
            saleOrder.setGiven(new BigDecimal("0"));
            saleOrder.setIsReconciliation(0);
            saleOrder.setIsTiaoPiao(0);
            saleOrder.setTiaoPiaoId(null);
            saleOrder.setMoLingTou(0);
            saleOrder.setIsWriteOff(0);
            saleOrder.setIsInvoice(0);
            saleOrderMapper.insert(saleOrder);
//            3、操作销售订单细节表,需要当前大表的流水号
            List<OtherProductsSaleBuyOrderDetailVo> otherProductsSaleOrderDetailList = otherProductsSaleOrderVo.getSaleOtherProductsOrderDetailList();
            ArrayList<SaleDetail> saleDetailArrayList = new ArrayList<>();
            for (int i = 0; i <otherProductsSaleOrderDetailList.size() ; i++) {
                OtherProductsSaleBuyOrderDetailVo orderDetailVo = otherProductsSaleOrderDetailList.get(i);
                SaleDetail saleDetail = new SaleDetail();
                saleDetail.setSaleOrderId(saleLsh);
                saleDetail.setSmallTypeId(orderDetailVo.getLeibie());
                saleDetail.setSpecification(orderDetailVo.getSpecification());
                saleDetail.setThickness(new BigDecimal("0"));
                saleDetail.setLong1(new BigDecimal("0"));
                saleDetail.setWide(new BigDecimal("0"));
                saleDetail.setHigh(new BigDecimal("0"));
                saleDetail.setQuantity(orderDetailVo.getQuantity());
                saleDetail.setPrice(orderDetailVo.getPrice());
                if (orderDetailVo.getWay()){
                    saleDetail.setWeight(orderDetailVo.getWeight());
                    saleDetail.setWay(1);
                }else {
                    saleDetail.setWeight(new BigDecimal("0"));
                    saleDetail.setWay(3);
                }
                saleDetailArrayList.add(saleDetail);
            }

            for (int i = 0; i <saleDetailArrayList.size() ; i++) {
                SaleDetail saleDetail = saleDetailArrayList.get(i);
                saleDetailOrderMapper.insert(saleDetail);
            }
//            4、对库存表进行一系列的更新
            for (int i = 0; i <saleDetailArrayList.size() ; i++) {
                SaleDetail saleDetail = saleDetailArrayList.get(i);
                QueryWrapper<Inventory> firstWrapper = OtherProductsWrapperUtil(saleDetail,3,null );
                List<Inventory> inventoryList = inventoryMapper.selectList(firstWrapper);
                if (!inventoryList.isEmpty()){
                    Integer temp=0;
                    Integer quantity = saleDetail.getQuantity();
                    for (int j = 0; j <inventoryList.size() ; j++) {
                        Inventory inventory = inventoryList.get(j);
                        temp = inventory.getQuantity() - quantity;
                        if (temp>0){
                            inventory.setQuantity(temp);
                            inventoryMapper.updateById(inventory);
                            break;
                        }else if (temp==0){
                            inventoryMapper.deleteById(inventory.getInventoryId());
                            break;
                        }else {
                            quantity=-temp;
                            inventoryMapper.deleteById(inventory.getInventoryId());
                        }
                    }
                    if (temp<0){
                        Inventory inventory2 = new Inventory();
                        inventory2.setSmallTypeId(saleDetail.getSmallTypeId());
                        inventory2.setWarehouseId(3);
                        inventory2.setSpecification(saleDetail.getSpecification());
                        inventory2.setThickness(saleDetail.getThickness());
                        inventory2.setLong1(saleDetail.getLong1());
                        inventory2.setWide(saleDetail.getWide());
                        inventory2.setHigh(saleDetail.getHigh());
                        inventory2.setQuantity(temp);
                        inventory2.setWeight(new BigDecimal(0));
                        inventoryMapper.insert(inventory2);
                    }

                }else {
                    QueryWrapper<Inventory> secondWrapper = OtherProductsWrapperUtil(saleDetail, null, 3);
                    List<Inventory> inventoryList2 = inventoryMapper.selectList(secondWrapper);
                    if (inventoryList2.isEmpty()){
                        Inventory inventory3 = new Inventory();
                        inventory3.setSmallTypeId(saleDetail.getSmallTypeId());
                        inventory3.setWarehouseId(3);
                        inventory3.setSpecification(saleDetail.getSpecification());
                        inventory3.setThickness(saleDetail.getThickness());
                        inventory3.setLong1(saleDetail.getLong1());
                        inventory3.setWide(saleDetail.getWide());
                        inventory3.setHigh(saleDetail.getHigh());
                        inventory3.setQuantity(-saleDetail.getQuantity());
                        inventory3.setWeight(new BigDecimal(0));
                        inventoryMapper.insert(inventory3);
                    }else {
                        Inventory inventory4 = inventoryList2.get(0);
                        Integer oldQuantity = inventory4.getQuantity();
                        Integer newQuantity=oldQuantity-saleDetail.getQuantity();
                        inventory4.setQuantity(newQuantity);
                        inventoryMapper.updateById(inventory4);
                    }
                }

            }
            msg.setMsg("添加其他类型产品销售订单成功");
            msg.setCode(1);
            return  msg;
        }else {
            msg.setMsg("系统好像睡着了，注册新供应商失败");
            msg.setCode(0);
            return msg;
        }
    }

    @Override
    public List<SaleOrder> saleFindRecoding(SaleByRecoding selectMsg) {
        Integer now = selectMsg.getCurrentPage();
        Integer size = selectMsg.getPageSize();
        now=(now-1)*size;

        Integer bigTypeId = selectMsg.getBigTypeId();
        Integer smallTypeId = selectMsg.getSmallTypeId();
        String specification = selectMsg.getSpecification();
        if (specification != null && !specification.equals("")){
            specification="%"+specification+"%";
        }
        String clientName = selectMsg.getClientName();
        if (clientName != null && !clientName.equals("")){
            clientName="%"+clientName+"%";
        }
        Boolean showAllPayInvoice = selectMsg.getShowAllPayInvoice();
        Boolean showPay = selectMsg.getShowPay();
        Boolean showInvoice = selectMsg.getShowInvoice();
        List<java.sql.Date> timeZone = selectMsg.getTimeZone();
        java.sql.Date startDate =null;
        java.sql.Date endDate =null;
        if (timeZone!=null){
            startDate = timeZone.get(0);
            endDate = timeZone.get(1);
        }


        return saleOrderMapper.findALlMsg(bigTypeId, smallTypeId, specification, clientName, showAllPayInvoice, showPay, showInvoice, startDate, endDate);
    }

    @Override
    public Integer saleFindRecodingCount(SaleByRecoding selectMsg) {
        Integer now = selectMsg.getCurrentPage();
        Integer size = selectMsg.getPageSize();
        now=(now-1)*size;

        Integer bigTypeId = selectMsg.getBigTypeId();
        Integer smallTypeId = selectMsg.getSmallTypeId();
        String specification = selectMsg.getSpecification();
        if (specification != null && !specification.equals("")){
            specification="%"+specification+"%";
        }
        String clientName = selectMsg.getClientName();
        if (clientName != null && !clientName.equals("")){
            clientName="%"+clientName+"%";
        }
        Boolean showAllPayInvoice = selectMsg.getShowAllPayInvoice();
        Boolean showPay = selectMsg.getShowPay();
        Boolean showInvoice = selectMsg.getShowInvoice();
        List<java.sql.Date> timeZone = selectMsg.getTimeZone();
        java.sql.Date startDate =null;
        java.sql.Date endDate =null;
        if (timeZone!=null){
            startDate = timeZone.get(0);
            endDate = timeZone.get(1);
        }
        return saleOrderMapper.saleFindRecodingCount(bigTypeId, smallTypeId, specification, clientName, showAllPayInvoice, showPay, showInvoice, startDate, endDate);

    }



}
